//
//  YTFPictureView.swift
//  sinaWeibo
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit


//定义一个间距
let ItemMargin: CGFloat = 10
//定义一个itemWidth
let itemWH = (UIScreen.mainScreen().bounds.width - 4 * ItemMargin)/3
//cell的重用标识
let REUSEDID = "cell"

class YTFPictureView: UICollectionView ,UICollectionViewDataSource{
    
    //赋值的时候，知道已经计算好的宽高，然后更新约束
    //这个是原创微博的
//    var pictureViewModel: YTFStatusViewModel?{
//        didSet{
//            
//            //更新配图的约束
//            self.snp_updateConstraints { (make) -> Void in
//                
//                let size = self.calPictureSize()
//                make.width.equalTo(size.width)
//                make.height.equalTo(size.height)
//                
//            }
//            
//            //赋值之后，应该需要重新加载数据
//            reloadData()
//            
//        }
//    }
    
    //对外统一一个数组，这个数组，转发微博和原创微博都可以传递数据
    var pictureList: [YTFPictureViewModel]?{
        
        didSet{
            //更新配图约束
            self.snp_updateConstraints { (make) -> Void in
                
                let size = self.calPictureSize()
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            
            //赋值之后，重新加载数据
            reloadData()
        }
    }
    
    
    //传递数据
    //不能添加lazy？
    var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        //设置流水布局
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件，设置约束
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        //实现collectionview的数据源方法
        self.dataSource = self
        
        //注册cell
        self.registerClass(YTFPictureViewCollectinCell.self, forCellWithReuseIdentifier: REUSEDID)
        
        //设置cell的大小
        flowLayout.itemSize = CGSizeMake(itemWH - 1 , itemWH - 1)
        flowLayout.minimumLineSpacing = ItemMargin
        flowLayout.minimumInteritemSpacing = ItemMargin
    }
    
    
    //计算配图视图的宽高
    private func calPictureSize() -> CGSize{
        
        //根据图片的个数
//        let count = pictureViewModel?.statusModel?.pic_urls?.count
        let count = pictureList?.count
        
        if count <= 3 {
            
            return CGSizeMake(itemWH * 3 + 2 * ItemMargin, itemWH)
            
        }else if count == 4 {
            //四张图片是以2X2的形式排列
            return CGSizeMake(itemWH * 2 + ItemMargin, itemWH * 2 + ItemMargin)
            
        }else {
            
            let row = CGFloat((count! - 1) / 3 + 1)
            return CGSizeMake(kUIScreenWidth - 2 * ItemMargin, (ItemMargin + itemWH) * row)
        }
        
    }
}

//实现数据源方法
extension YTFPictureView{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
//        return pictureViewModel?.statusModel?.pic_urls?.count ?? 0
        return pictureList?.count ?? 0
        
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(REUSEDID, forIndexPath: indexPath) as! YTFPictureViewCollectinCell
        
        //设置随机色
//        cell.backgroundColor = UIColor(red: CGFloat(random()%255)/255.0, green: CGFloat(random()%255)/255.0, blue: CGFloat(random()%255)/255.0, alpha: 1.0)
//        cell.backgroundColor = randomColor()
    
        
        cell.pictureViewModel = pictureList![indexPath.item]
        
        return cell
    }
    
}


//自定义一个cell类型
class YTFPictureViewCollectinCell: UICollectionViewCell {
    
    //传值
    var pictureViewModel: YTFPictureViewModel?{
        
        didSet{
            
            let url = NSURL(string: pictureViewModel?.model?.thumbnail_pic ?? "")
            
            if let Ourl = url {
                iconImageView.sd_setImageWithURL(Ourl)
            }
            
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        addSubview(iconImageView)
        
        iconImageView.frame = self.bounds
    }
    
    //懒加载
    private lazy var iconImageView: UIImageView = {
        
        let img = UIImageView()
        
        return img
        
    }()

}







